package com.javer.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.javer.domain.Comment;
import com.javer.domain.PageView;
import com.javer.service.CommentService;
import com.javer.util.WebUtil;

@Controller("/comment")
public class CommentAction extends DispatchAction {
	final Log logger = LogFactory.getLog(getClass());
	@Autowired private CommentService commentService;
	//@Resource private CompanyService companyService;

	public ActionForward getCommentByTid(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		int tid = Integer.valueOf(req.getParameter("tid"));
		String ctype = req.getParameter("ctype");
		PageView<Comment> pv = WebUtil.createPageView(req);
		req.setAttribute("pv", commentService.getPageByTid(pv, tid, ctype));
		return mapping.findForward("list-comment");
	}
	
	public ActionForward goCommentDetailPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		int tid = Integer.valueOf(req.getParameter("tid"));
		String ctype = req.getParameter("ctype");
		PageView<Comment> pv = WebUtil.createPageView(req);
		req.setAttribute("pv", commentService.getPageByTid(pv, tid, ctype));
		return new ActionForward("/pages/comment/commentdetail.jsp");
	}
	
	public ActionForward getByPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		PageView<Comment> pv = WebUtil.createPageView(req);
		req.setAttribute("pv", commentService.getByPage(pv));
		return mapping.findForward("list-comment");
	}
	
	/**
	 * 支持或是反对操作
	 */
	public ActionForward modifyCommentForAgree(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		Comment c = new Comment();
		BeanUtils.populate(c, req.getParameterMap());
		commentService.updateForAgree(c);
		return null;
	}
	
}
